﻿using PortfolioApp.Data;
using PortfolioApp.DataModels;
using PortfolioApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PortfolioApp.Web.DbAdapters
{
    public class PortfolioAppAdapter : IPortfolioAppAdapter
    {

        public Post CreatePost(Post post)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                post.TimeStamp = DateTime.Now.ToBinary();
                post = db.Posts.Add(post);
                db.SaveChanges();
            }
            return post;
        }

        public Post GetPost(int id)
        {
            Post post;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                post = db.Posts.Find(id);
            }
            return post;
        }

        public List<Post> GetAllPosts()
        {
            List<Post> posts;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                posts = db.Posts.OrderBy(x => x.TimeStamp).ToList();
            }
            return posts;
        }
        public void GetPostsForPage(PostIndexVM vm)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                vm.LoadPostsForPage(db);
            }
        }

        public List<Post> GetPostsForUser(string id)
        {
            List<Post> posts;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                posts = db.Posts.Where(x => x.ApplicationUserId == id).OrderBy(x => x.TimeStamp).ToList();
            }
            return posts;
        }

        public Post UpdatePost(Post post)
        {
            Post dbPost;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbPost = db.Posts.Find(post.Id);
                dbPost.SetProperties(post);
                db.SaveChanges();
            }
            return dbPost;
        }

        public Post DeletePost(Post post)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                post = db.Posts.Find(post.Id);
                db.Posts.Remove(post);
                db.SaveChanges();
            }
            return post;
        }

        public Project CreateProject(Project project)
        {
            throw new NotImplementedException();
        }

        public Project GetProject(int id)
        {
            throw new NotImplementedException();
        }

        public List<Project> GetAllProjects()
        {
            throw new NotImplementedException();
        }
        public void GetProjectsForPage(ProjectIndexVM vm)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                vm.LoadProjectsForPage(db);
            }
        }
        public List<Project> GetProjectsForUser(string id)
        {
            throw new NotImplementedException();
        }

        public Project UpdateProject(Project project)
        {
            throw new NotImplementedException();
        }

        public Project DeleteProject(Project project)
        {
            throw new NotImplementedException();
        }

        public string GetUserName(string id)
        {
            ApplicationUser user;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                user = db.Users.Find(id);
            }
            return user.UserName;
        }
    }
}